<template>

    <div class="login-container">
        <div class="container">
            <div>
                <div class="form-container sign-up-container">
                    <div class="login-title">账号密码登录</div>
                    <el-form
                        ref="loginFormRef"
                        :model="loginDataRef"
                        :rules="loginRulesCom"
                    >
                        <el-form-item prop="username">
                            <el-input
                                ref="username"
                                v-model="loginDataRef.username"
                                style="width: 240px"
                                :placeholder="$t('login.username')"
                                :prefix-icon="UserFilled"
                            />
                        </el-form-item>

                        <el-form-item prop="password">
                            <el-input
                               v-model="loginDataRef.password"
                                type="password"
                                name="password"
                                :placeholder="$t('login.password')"
                                style="width: 240px"
                                :prefix-icon="Lock"
                                show-password
                                @keyup.enter="handleLogin"
                            />
                        </el-form-item>

                        <el-button @click="handleLogin" class="btn-grad"
                            >登&nbsp;&nbsp;&nbsp;&nbsp;录</el-button
                        >
                    </el-form>
                </div>
            </div>

            <div>
                <div class="overlay-container">
                    <div class="overlay">
                        <div class="overlay-panel overlay-left">
                            <span class="welcome">欢迎登录</span>
                            <!-- <span class="title">YICR 宽带接入规划</span>
                            <span class="title">分析平台</span> -->
                            <!-- <span>宽带接入规划数据分析</span>
                            <span>及图形化展示系统</span> -->
                            <span class="title">YICR 极简无线网络</span>
                            <span class="title">工具平台</span>
                            <span class="support">技术支持：业务支撑中心</span>
                        </div>
                        <!-- <div class="overlay-panel overlay-right">
                            <h1>Hello, Buddy!</h1>
                            <p>Join Us on a new adventure</p>
                            <div class="btn-grad" id="signUp">Sign Up</div>
                        </div> -->
                    </div>
                </div>
            </div>
        </div>
        <div class="footer">
            <div>
                <img src="@/assets/images/login/yicr_logo.png" />
            </div>
            <div>
                <p style="font-weight: 700; font-size: 1.1rem">
                    河南省信息咨询设计研究有限公司
                </p>
                <p style="font-size: 1rem; color: #666">
                    原河南省电信规划设计院  
                     <!-- login index
                    {{ helloRef.msg }} -->
                </p>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import * as api from '@/api/test/test_api'
import { Hide, Lock, UserFilled, View } from "@element-plus/icons-vue";
import { TypeLoginDataI } from "@/api/auth/auth_types";
import { computed, ref,onMounted } from "vue";
import { ElForm, FormRules } from "element-plus";
import { useAppStore } from "@/store/modules/app_store";
import { useI18n } from "vue-i18n";
import * as authApi from '@/api/auth/auth_api_index'
import { LocationQuery, LocationQueryValue, useRoute } from "vue-router";
import { useUserStore } from "@/store/modules/user_store";
import { router } from "@/routes/routes_index";
const appStore = useAppStore();



const loginDataInit:TypeLoginDataI={
    // username:"admin",
    // password:"123456"
    username:"",
    password:""
}
const { t } = useI18n();

const captchaBase64Ref = ref(); // 验证码图片Base64字符串
const loadingRef = ref(false); // 按钮loading
const loginFormRef = ref(ElForm); // 登录表单ref
const loginDataRef = ref(Object.assign({},loginDataInit));

const loginRulesCom = computed<FormRules<typeof loginDataRef.value>>(() => {
  const prefix = appStore.languageRef === "en" ? "Please enter " : "请输入";
  return {
    username: [
      {
        required: true,
        trigger: "blur",
        message: `${prefix}${t("login.username")}`,
      },
    ],
    password: [
      {
        required: true,
        trigger: "blur",
        validator: (rule: any, value: any, callback: any) => {
          if(!value || value===''){
            callback(new Error( `${prefix}${t("login.password")}`))
          }  else if (value.length < 6) {
            // callback(new Error("The password can not be less than 6 digits"));
            callback(new Error(`${t("login.password")}${t("length.less6")}`));
          } else {
            callback();
          }
        },
        // message: `${prefix}${t("login.password")}`,
      },
    ],
    captchaCode: [
      {
        required: true,
        trigger: "blur",
        message: `${prefix}${t("login.captchaCode")}`,
      },
    ],
  };
});

/**
 * 获取验证码
 */
 function getCaptcha() {
    authApi.getCaptchaApi().then(({ data }) => {
    loginDataRef.value.captchaKey = data.captchaKey;
    captchaBase64Ref.value = data.captchaBase64;
  });
}

/**
 * 登录
 */
const route = useRoute();
const userStore = useUserStore();
function handleLogin() {
    console.log('in handleLogin');
    
  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loadingRef.value = true;
      userStore
        .login(loginDataRef.value)
        .then(() => {
          const query: LocationQuery = route.query;

          const redirect = (query.redirect as LocationQueryValue) ?? "/";

          const otherQueryParams = Object.keys(query).reduce(
            (acc: any, cur: string) => {
              if (cur !== "redirect") {
                acc[cur] = query[cur];
              }
              return acc;
            },
            {}
          );

          router.push({ path: redirect, query: otherQueryParams });
        })
        .catch(() => {
          // 验证失败，重新生成验证码 
          //todo:
          //getCaptcha();
        })
        .finally(() => {
          loadingRef.value = false;
        });
    }
  });
}



const helloRef=ref({
    msg:'init'
})

onMounted(async ()=>{
    console.log('in login onMounted');
    const respone=await api.getHelloTest();
    console.log('respone',respone);
    
    helloRef.value=respone.data;
})


</script>
<style lang="scss" scoped>
.login-container {
    //@include self_fill;
    //background: url("@/assets/images/bg_1.png") no-repeat center center;
    // background-size: contain;
    // background-size: cover;
    //background-size: 100% 100vh;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #4568dc; /* fallback for old browsers */
    background: linear-gradient(to right, #6f98c6, #4568dc);
    background: url("@/assets/images/login/bg_08.png") no-repeat center center;
    // background-size: 100% 100vh;
    .container {
        background-color: #ffffff;
        border-radius: 10px;
        box-shadow: 0 5px 10px rgba(0, 0, 0, 0.25),
            0 5px 5px rgba(0, 0, 0, 0.22);
        position: relative;
        overflow: hidden;
        width: 768px;
        min-width: 600px;
        max-width: 100%;
        min-height: 450px;

        .form-container {
            position: absolute;
            top: 0;
            height: 100%;
            transition: all 0.6s ease-in-out;

            .login-title {
                position: absolute;
                left: 50%;
                transform: translate(-50%, 0);
                margin-top: 50px;
                color: #748cdd;
                font-size: 1.4rem;
                font-weight: 600;
                letter-spacing: 5px;
            }
            .inputBx {
                position: relative;
                margin-bottom: 15px;
            }

            .inputBx input {
                width: 100%;
                outline: none;
                border: 1px solid rgba(255, 255, 255, 0.2);

                background: #fff;
                padding: 8px 10px;
                padding-left: 35px;
                border-radius: 6px;
                color: black;
                font-size: 14px;
                font-weight: 300;
                box-shadow: inset 0 0 25px rgba(164, 158, 158, 0.2);
            }

            .inputBx input:focus {
                border: 1px solid rgba(17, 86, 225, 0.8);
            }
            .inputBx img {
                position: absolute;
                top: 17px;
                left: 10px;
                // transition: scale(0.7);
                filter: invert(0.6);
            }
            .btn-grad {
                background-image: linear-gradient(
                    to right,
                    #6f84ef 0%,
                    #546ed7 51%,
                    #3c3cbe 100%
                );
            }
            .btn-grad {
                margin: 10px;
                margin-top: 25px;
                padding: 6px 65px;
                text-align: center;
                // text-transform: uppercase;
                transition: 0.5s;
                background-size: 200% auto;
                color: white;
                border-radius: 10px;
                display: block;
                cursor: pointer;
            }

            .btn-grad:hover {
                background-position: right center; /* change the direction of the change here */
                color: #fff;
                text-decoration: none;
            }
        }

        .sign-in-container {
            left: 50%;
            width: 50%;
            opacity: 0;
            z-index: 2;
        }

        .sign-up-container {
            left: 50%;
            width: 50%;
            z-index: 1;
        }
    }

    .overlay-container {
        position: absolute;
        top: 0;
        left: 0%;
        width: 50%;
        height: 100%;
        overflow: hidden;
        transition: transform 0.6s ease-in-out;
        z-index: 100;
        letter-spacing: 3px;
        .overlay {
            // background: #6441a5;
            // background: -webkit-linear-gradient(to right, #6441a5, #2a0845);
            // background: linear-gradient(to right, #5e5bf1, #2a0845);
            background: linear-gradient(to right, #2f72e5, #2a0845);
            background-repeat: no-repeat;
            background-size: cover;
            background-position: 0 0;
            color: #ffffff;
            position: relative;
            left: 0;
            height: 100%;
            width: 200%;
            transform: translateX(0);
            transition: transform 0.6s ease-in-out;
            .overlay-panel {
                position: absolute;
                display: flex;
                align-items: center;
                justify-content: center;
                flex-direction: column;
                padding: 0 40px;
                text-align: center;
                top: 0;
                height: 100%;
                width: 50%;
                transform: translateX(0);
                transition: transform 0.6s ease-in-out;
                .title {
                    font-size: 1.8rem;
                    font-weight: bold;
                    margin: 0;
                    margin-bottom: 5px;
                }
                .welcome {
                    position: absolute;
                    left: 40px;
                    top: 50px;
                    font-size: 1.2rem;
                    color: #ddd;
                }
                .support {
                    position: absolute;
                    bottom: 25px;
                    color: #ddd;
                }
                p {
                    font-size: 14px;
                    font-weight: 100;
                    line-height: 20px;
                    letter-spacing: 0.5px;
                    margin: 20px 0 30px;
                }
            }
        }
    }

    form {
        background-color: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        padding: 0 50px;
        height: 100%;
        text-align: center;
        input {
            background-color: #eee;
            border: none;
            padding: 12px 15px;
            margin: 8px 0;
            width: 100%;
        }
    }

    .footer {
        position: absolute;
        display: flex;
        align-items: center;
        bottom: 50px;
        left: 50%;
        transform: translateX(-50%);
        text-align: center;
    }
    .footer img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
    }
}
</style>
